package com.zeng.niceservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.nicecore.constant.IgnoreConstant;
import com.zeng.nicecore.util.BeanUtil;
import com.zeng.nicecore.util.IgnoreStringUtil;
import com.zeng.nicedao.mapper.FileServerMapper;
import com.zeng.nicepojo.entity.FileServer;
import com.zeng.nicepojo.vo.FileServerVo;
import com.zeng.niceservice.service.FileServerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class FileServerServiceImpl implements FileServerService {

    @Autowired
    FileServerMapper fileServerMapper;

    @Override
    public FileServerVo addOne(FileServerVo fileServerVo) {
        FileServer fileServer = FileServer.builder().build();
        BeanUtils.copyProperties(fileServerVo, fileServer, IgnoreStringUtil.ignoreExclude());
        int insert = fileServerMapper.insert(fileServer);
        return BeanUtil.beanToVo(fileServer, fileServerVo);
    }

    @Override
    public FileServerVo getOneByCode(String code) {

        FileServer fileServer = this.fileServerMapper.selectById(code);
        FileServerVo fileServerVo = BeanUtil.beanToVo(fileServer, new FileServerVo());

        return fileServerVo;
    }

    @Override
    public Integer update(FileServerVo fileServerVo) {
        FileServer fileServer = BeanUtil.VoToBean(fileServerVo,
                new FileServer(),
                IgnoreStringUtil.ignoreExclude(IgnoreConstant.code.getValue()));
        return this.fileServerMapper.updateById(fileServer);
    }

    @Override
    public Integer delete(String code) {
        return this.fileServerMapper.deleteById(code);
    }

    @Override
    public Integer batchDelete(List<String> codes) {

        return this.fileServerMapper.deleteBatchIds(codes);
    }

    @Override
    public List<FileServerVo> findPage(Page page, FileServerVo fileServerVo) {
        FileServer fileServer = BeanUtil.VoToBean(fileServerVo, new FileServer());
        this.fileServerMapper.selectPage(page, this.getWrapper(fileServerVo));
        List orders = page.getRecords();
        List<FileServerVo> fileServerVos = new ArrayList<>();
        orders.forEach(e->{
            fileServerVos.add(BeanUtil.beanToVo(e, new FileServerVo()));
        });
        page.setRecords(null);
        return fileServerVos;
    }


    @Override
    public Integer count(FileServerVo fileServerVo) {
        Integer integer = this.fileServerMapper.selectCount(this.getWrapper(fileServerVo));
        return integer;
    }

    public AbstractWrapper getWrapper(FileServerVo fileServerVo) {
        QueryWrapper<FileServer> wrapper = new QueryWrapper<>();
        if (fileServerVo != null) {
            if (StringUtils.isNotEmpty(fileServerVo.getName())) {
                wrapper.like("name", fileServerVo.getName());
            }
            if (StringUtils.isNotEmpty(fileServerVo.getFlag())) {
                wrapper.like("flag", fileServerVo.getFlag());
            }
            if (StringUtils.isNotEmpty(fileServerVo.getType())) {
                wrapper.like("type", fileServerVo.getType());
            }
        }
        return wrapper;
    }
}
